home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15845 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  86 lines

  1. Path: news.th-darmstadt.de!news
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: const member functions
  5. Date: 08 Apr 1996 15:40:46 +0200
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Sender: enno@kitz.inferenzsysteme.informatik.th-darmstadt.de
  8. Message-ID: <ltk9zqal5t.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <316588E6.7D61@geoplex.com>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: "Jay B. Perry"'s message of Fri, 05 Apr 1996 12:56:06 -0800
  12. X-Newsreader: Gnus v5.1
  13.  
  14. In article <316588E6.7D61@geoplex.com> "Jay B. Perry" <jay@geoplex.com> writes:
  15.  
  16.    The attached code doesn't compile, complaining about the non-const
  17.    object's attempt to use a non-const private method (even though a
  18.    const public method with the same name is available).
  19.  
  20.    After leafing through a couple of references I have on C++, I have
  21.    come to the conclusion that the attached C++ code should be
  22.    compilable.
  23.  
  24.    Could someone explain to me why this code doesn't work?  I have
  25.    tried compiling it using VC++4.1, SunSoft CC SC3.0.1, g++ 2.7.x
  26.    (I am not sure what the current version is, but I know that the
  27.    most current version was used).
  28.  
  29.    My theory is that it is bagging it because it can't differentiate
  30.    between the int& and the int.  But, I also had hoped that in the
  31.    context of the call, it should be able to realize that it doesn't
  32.    have access to the private method, so let's see if there is a public
  33.    method that can be used.
  34.  
  35.    As far as options, I just attempted to produce an executable:
  36.  
  37.    g++ -o file file.cc  (using g++ as an example)
  38.  
  39.    //
  40.    //  Code starts here
  41.    //
  42.    #include <stdlib.h>
  43.    #include <iostream.h>
  44.  
  45.    class X 
  46.    {
  47.    public:   //  Public methods
  48.       X( )           : _value( 0 ) { }
  49.       X( int value ) : _value( value ) { }
  50.  
  51.       //  Public accessor, should be available to all
  52.       //  X objects.
  53.       int value( ) const { return _value; }
  54.  
  55.    private:  //  Private methods
  56.       //  Private accessor, should only be available to
  57.       //  non-const X objects
  58.       int& value( ) { return _value; }
  59.  
  60.    private:  //  Private data members
  61.       int _value;
  62.    };
  63.  
  64.    main( )
  65.    {
  66.       X t1( 1 );
  67.       const X t2( 2 );
  68.  
  69.       //  The following line results in an error for several
  70.       //  compilers, all complaining about an attempt to use
  71.       //  the private X::value method!
  72.       cout << t1.value() << endl;
  73.  
  74.       //  This line
  75.       cout << t2.value() << endl;
  76.    }
  77.  
  78. 't1' is declared as non-const object of class 'X', both versions of
  79. 'X::value' are visible and access-control takes place after an
  80. appropriate function is selected.
  81. Thus 't1.value()' tries to call the non-const 'X::value' which results
  82. in an error.
  83.  
  84.         Enno
  85.  
  86.